(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:pager
     * @description
     * # pager 
     */
    angular.module('webApp')
        .directive('pager', function() {
            return {
                restrict: 'E',
                templateUrl: "views/directives.pager.html",
                scope: {
                    pageNum: "@",
                    pageSize: "=",
                    pageNo: "@",
                    total: "=",
                    getData: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    $scope.$watchGroup(['total', 'pageNo'], function(newValue, oldValue, scope) {
                        $scope.pageNum = Number($scope.pageNum || 5);
                        $scope.pageSize = Number($scope.pageSize || 10);
                        $scope.pageNo = Number($scope.pageNo || 1);
                        $scope.total = Number($scope.total || 0);
                        // console.log("total:", $scope.total, "pageSize:", $scope.pageSize, "pageNo:", $scope.pageNo);
                        $scope.totalPage = Math.ceil($scope.total / $scope.pageSize);
                        // console.log("totalPage:", $scope.totalPage);

                        //前半部分
                        $scope.pageA = [];
                        var pstart = $scope.pageNo - $scope.pageNum;
                        pstart = pstart <= 0 ? 1 : pstart;
                        for (var i = pstart; i < $scope.pageNo; i++) {
                            $scope.pageA.push(i);
                        }

                        //后半部分
                        $scope.pageB = [];
                        var pend = $scope.pageNo + $scope.pageNum;
                        pend = pend > $scope.totalPage ? $scope.totalPage : pend;
                        for (var i = $scope.pageNo + 1; i <= pend; i++) {
                            $scope.pageB.push(i);
                        }
                        // console.log("pstart:", pstart, "pend:", pend);
                        // console.log("前：", $scope.pageA, "后：", $scope.pageB);
                        $scope.pageList = _.union($scope.pageA, [$scope.pageNo], $scope.pageB);
                        // console.log("页码：", $scope.pageList);
                        //判断是否有前后隐藏的页码
                        $scope.haveA = $scope.pageList[0] != 1;
                        $scope.haveB = $scope.pageList[$scope.pageList.length - 1] != $scope.totalPage;
                    });

                    $scope.$watch('inputNo', function(newValue, oldValue, scope) {
                        if (newValue > $scope.totalPage) {
                            $scope.inputNo = Number($scope.totalPage);
                        }
                        if (newValue < 1) {
                            $scope.inputNo = 1;
                        }
                    });

                    $scope.goPage = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        pageNo = pageNo > $scope.totalPage ? $scope.totalPage : pageNo;
                        $scope.pageNo = pageNo;
                        if ($scope.getData) {
                            $scope.getData({ pageNo: pageNo});
                        }
                    }

                    function setPageNo(pageNo) {
                        pageNo = pageNo < 1 ? 1 : pageNo;
                        pageNo = pageNo > $scope.totalPage ? $scope.totalPage : pageNo;
                        return pageNo;
                    }
                }]
            }
        });
})();
